﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NppPluginNET
{
    public class PluginExtensionParser
    {
        private Dictionary<string, ExtensionLanguage> extensionLanguages;

        private Dictionary<string, string> speechExtensions;

        public PluginExtensionParser(Dictionary<string, ExtensionLanguage> extensionLanguages, Dictionary<string, string> speechExtensions)
        {
            this.extensionLanguages = extensionLanguages;
            this.speechExtensions = speechExtensions;
        }

        private int GetCursorPos(string text)
        {
            if(!text.Contains('#'))
                return text.Length;
            else
                return text.IndexOf('#'); 

        }

        private ParseResult ParseCursorOnlyStatement(string text)
        {
            int cursorPos = GetCursorPos(text);
            text = text.Replace("#", ""); // remove cursor char

            return new ParseResult(text, cursorPos);
        }

        private ParseResult ParseSingleWildcardStatement(string text,string replacement)
        {
            int cursorPos = GetCursorPos(text);
            text = text.Replace("#", ""); // remove cursor char

            //replace wildcard with replacement
            text = text.Replace("%1", replacement);

            return new ParseResult(text, cursorPos);
        }

        private ParseResult ParseDoubleWildcardStatement(string text, string replacement1, string replacement2)
        {
            int cursorPos = GetCursorPos(text);
            text = text.Replace("#", ""); // remove cursor char

            //replace wildcard with replacement
            text = text.Replace("%1", replacement1);
            text = text.Replace("%2", replacement2);

            return new ParseResult(text, cursorPos);
        }

        private ParseResult ParseTripleWildcardStatement(string text, string replacement1, string replacement2, string replacement3)
        {
            int cursorPos = GetCursorPos(text);
            text = text.Replace("#", ""); // remove cursor char

            //replace wildcard with replacement
            text = text.Replace("%1", replacement1);
            text = text.Replace("%2", replacement2);
            text = text.Replace("%3", replacement3);

            return new ParseResult(text, cursorPos);
        }
        
        private string getLanuguageName(LangType languageType)
        {
            if (languageType.Equals(LangType.L_CS))
                return "C#";
            else if (languageType.Equals(LangType.L_JAVA))
                return "Java";
            else if (languageType.Equals(LangType.L_PASCAL))
                return "Pascal";
            else
                throw new PluginExtensionExeption("Language type not supported");

        }

        private ExtensionLanguage GetExtensionLanguge(LangType languageType)
        {
            string languageName = getLanuguageName(languageType);
            languageName = extensionLanguages.Keys.ToList().Find(x => x == languageName);
            if(languageName == null)
                throw new PluginExtensionExeption("Language type not supported");
            return extensionLanguages[languageName];
        }


        public ParseResult ParseIfText(LangType language)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseCursorOnlyStatement(extLanguage.IfText);
        }

        public ParseResult ParseIfElseText(LangType language)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseCursorOnlyStatement(extLanguage.IfElseText);
        }

        public ParseResult ParseForText(LangType language)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseCursorOnlyStatement(extLanguage.ForText);
        }

        public ParseResult ParseWhileText(LangType language)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseCursorOnlyStatement(extLanguage.WhileText);
        }

        public ParseResult ParseClassText(LangType language, string access, string name)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseDoubleWildcardStatement(extLanguage.ClassText, access, name);
        }

        public ParseResult ParseConstructorText(LangType language, string name)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseSingleWildcardStatement(extLanguage.ConstructorText, name);
        }

        public ParseResult ParseGetterSetterText(LangType language, string type, string name)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseDoubleWildcardStatement(extLanguage.GetterSetterText,type, name);
        }

        public ParseResult ParseFunctionText(LangType language, string access, string returnType, string name)
        {
            ExtensionLanguage extLanguage = GetExtensionLanguge(language);
            return ParseTripleWildcardStatement(extLanguage.FunctionText, access, returnType, name);
        }

        public string ParseSpeechToText(string name)
        {
            if (!speechExtensions.ContainsKey(name))
                return null;
            return speechExtensions[name];
        }
    }
}
